package com.lwx.leetcode.offer.middile;

public class Offer14 {
    public static void main(String[] args) {
        Offer14 o = new Offer14();
        System.out.println(o.cuttingRope(10));
    }
    public int cuttingRope(int n) {
       return maxLen(n);
    }

    //返回长度为n的绳子的最大乘积
    public int maxLen(int n){
        if(n<=0){
            return 0;
        }
        int[] dp = new int[n+1];
        if(n == 1){
            dp[1] =1;
        }
        if(n == 2){
            dp[2] =1;
        }
        //至少切出两段
        for (int i = 3; i <= n; i++) {
            for (int j = 1; j <i-1 ; j++) {
                dp[i] = Math.max(dp[i], Math.max(j*dp[i-j], j*(i-j)));
//                dp[i] = Math.max(dp[i], Math.max(j*dp[i-j], j*(i-j)));
            }
        }
        return dp[n];
    }

    //返回长度为n的绳子的最大乘积
    public int maxLen1(int n){
        if(n<=0){
            return 0;
        }
        int[] dp = new int[n+1];
        if(n == 1){
            dp[1] =1;
        }
        if(n == 2){
            dp[2] =1;
        }
        //至少切出两段
        for (int i = 1; i <= n; i++) {
            for (int j = n-1; j > 0 ; j--) {
                dp[i] = j*maxLen1(i-j);
            }
        }
        int max = 0;
        return dp[n];
    }

}